/**
 * Copyright (C), 2021, 中电福富信息科技有限公司
 * FileName: TenantController
 * Author:   xujiajun
 * Date:     2021/3/1 17:08
 */
package com.example.demo.web.rest.controller;

import com.ffcs.oss.annotation.PermissionAuth;
import com.example.demo.client.SmQryClient;
import com.example.demo.client.evt.tenancy.QryTenancyRelateUserEvt;
import com.ffcs.oss.param.out.ServiceResp;
import com.example.demo.service.TenancyService;
import com.example.demo.web.rest.evt.TenancyEvt;
import com.example.demo.web.rest.evt.UserTenancyInfoEvt;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * Description:
 * @Author xujiajun
 * @Date 2021/3/1  17:08
 */
@Api("租户管理")
@RestController
@RequestMapping("/api/tenancy/")
public class TenancyController {

    protected Logger logger= LoggerFactory.getLogger(getClass());

    @Autowired
    private TenancyService tenancyService;

    @Autowired
    private SmQryClient csSmQrySrvClient;

    @ApiOperation("查询租户信息")
    @PostMapping("/listTenancyInfo")
    public ServiceResp listTenancyInfo(@RequestBody TenancyEvt evt) {
        return ServiceResp.getInstance().success(tenancyService.listTenancyInfo(evt));
    }

    @ApiOperation("查询用户信息关联租户表")
    @PostMapping("/listUserTenancyInfo")
    public ServiceResp listUserTenancyInfo(@RequestBody UserTenancyInfoEvt evt) {
        return ServiceResp.getInstance().success(tenancyService.listUserTenancyInfo(evt));
    }

    @ApiOperation("查询租户关联用户列表")
    @PostMapping("/qryTenancyRelateUserList")
    public ServiceResp qryTenancyRelateUserList(@RequestBody QryTenancyRelateUserEvt evt) {
        return csSmQrySrvClient.qryTenancyRelateUserList(evt);
    }


    @ApiOperation("新增租户信息")
    @PostMapping("/insertTenancyInfo")
    @PermissionAuth(code = "OSS-SETTING-TENANCY-ADD")
    public ServiceResp insertTenancyInfo(@RequestBody TenancyEvt evt) {
        try {

            Boolean result = tenancyService.insertTenancyInfo(evt);
            if (result) {
                return ServiceResp.getInstance().success("操作成功");
            } else {
                return ServiceResp.getInstance().error("操作失败");
            }
        }catch (Exception e) {
            logger.error("新增租户信息异常", e);
            return ServiceResp.getInstance().error("操作失败");
        }
    }

    @ApiOperation("删除租户信息")
    @PostMapping("/delTenancyInfo")
    @PermissionAuth(code = "OSS-SETTING-TENANCY-DEL")
    public ServiceResp delTenancyInfo(@RequestBody TenancyEvt evt) {
        Boolean result = tenancyService.delTenancyInfo(evt);
        if (result) {
            return ServiceResp.getInstance().success("操作成功");
        }else {
            return ServiceResp.getInstance().error("操作失败");
        }
    }

    @ApiOperation("修改租户信息")
    @PostMapping("/updTenancyInfo")
    public ServiceResp updTenancyInfo(@RequestBody TenancyEvt evt) {
        try {
            Boolean result = tenancyService.updTenancyInfo(evt);
            if (result) {
                return ServiceResp.getInstance().success("操作成功");
            }else {
                return ServiceResp.getInstance().error("操作失败");
            }
        }catch (Exception e) {
            logger.error("修改租户信息异常", e);
            return ServiceResp.getInstance().error("操作失败");
        }
    }

    @ApiOperation("保存租户信息")
    @PostMapping("/saveTenancyInfo")
    @PermissionAuth(code = "OSS-SETTING-TENANCY-EDIT,OSS-SETTING-TENANCY-ADD")
    public ServiceResp saveTenancyInfo(@RequestBody(required = true) TenancyEvt evt) {
        try {
            Boolean result = false;
            if (evt.getId() == null || evt.getId() <= 0) {
                result = tenancyService.insertTenancyInfo(evt);
            } else {
                result = tenancyService.updTenancyInfo(evt);
            }
            if (result) {
                return ServiceResp.getInstance().success("保存成功");
            } else {
                return ServiceResp.getInstance().error("保存失败");
            }
        }catch (Exception e) {
            logger.error("修改租户信息异常", e);
            return ServiceResp.getInstance().error(e.getMessage());
        }
    }

    @ApiOperation("修改租户状态以及备注")
    @PostMapping("/updTenancyStatusAndRemark")
    @PermissionAuth(code = "OSS-SETTING-TENANCY-AUDIT-EDIT,OSS-SETTING-TENANCY-CANCEL-EDIT")
    public ServiceResp updTenancyStatusAndRemark(@RequestBody TenancyEvt evt) {
        try {
            Boolean result = tenancyService.updTenancyStatusAndRemark(evt);
            if (result) {
                return ServiceResp.getInstance().success("操作成功");
            } else {
                return ServiceResp.getInstance().error("操作失败");
            }
        }catch (Exception e) {
            logger.error("修改租户状态以及备注异常", e);
            return ServiceResp.getInstance().error("操作失败");
        }
    }

}